<template>
    <div id="poster">
    <el-form :model="registForm" :rules="rules" ref="registForm" class="login-container" label-position="left" label-width="0px">
        <h3 class="login_title">账号注册</h3>
        <el-form-item prop="username">
            <el-input type="text" v-model="registForm.username" auto-complete="off" placeholder="用户账号"></el-input>
        </el-form-item>

        <el-form-item prop="password">
            <el-input type="password" v-model="registForm.password" auto-complete="off" placeholder="用户密码"></el-input>
        </el-form-item>


        <div class="left">
            <span class="head-img">上传头像</span>
            <el-upload
                    class="avatar-uploader"
                    action="String"
                    ref="upload"
                    :show-file-list="false"
                    :on-change="handleAvatarSuccess"
                    :http-request="uploadFile"
                    :auto-upload="false"
            >
                <img v-if="dialogImageUrl" :src="dialogImageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </div>
        <div class="right">
            <span class="text-sex">选择性别</span>
            <el-form-item class="sex" prop="sex">
                <el-select v-model="registForm.sex" placeholder="请选择性别">
                    <el-option label="女" value="女"></el-option>
                    <el-option label="男" value="男"></el-option>
                </el-select>
            </el-form-item>
        </div>

        <el-form-item style="width: 100%">
            <el-button type="primary" style="width: 100%;background: #0FF;border: none" v-on:click="regist('registForm')">立即注册</el-button>
        </el-form-item>

        <el-form-item style="width: 100%">
            <router-link to="/login">
            <el-button type="primary" style="width: 100%;background: #90EE90;border: none" @click="goLogin">返回登录</el-button>
            </router-link>
        </el-form-item>
    </el-form>
    </div>
</template>

<style scope>
    #poster {
        background-color:#333;
        height: 100%;
        width: 100%;
        background-size: cover;
        position: fixed;
    }
    .login-container {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 10vh auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }

    .login_title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
    }

    .avatar-uploader .el-upload {
        border: 2px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        margin-left: 130px;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 40px;
        color: #8c939d;
        width: 40px;
        height: 40px;
        text-align: center;
    }
    .avatar {
        width: 40px;
        height: 40px;
        display: block;

    }
    .left{
        margin-bottom: 30px;
    }
    .text-sex,.head-img{
        display: inline-block;
        font-weight: 800;
        font-size: 18px;
        float: left;
        line-height: 2em;
    }
    .sex{
        display: inline-block;
        /*float: right;*/
        margin-left: 50px;
    }
</style>


<script>
    import axios from 'axios';
    export default {
        data() {
            return {
                dialogImageUrl: '',
                formData: "",
                registForm: {
                    username: '',
                    password: '',
                    sex:'',
                    birthday:'',

                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ],
                    sex: [
                        { required: true, message: '请选择性别', trigger: 'blur' },
                    ],
                }
            }
        },
        methods: {
            goLogin:function () {
                this.$router.push('/register')
            },
            handleAvatarSuccess(file) {
                this.dialogImageUrl =URL.createObjectURL(file.raw);
            },
            uploadFile(file) {
                this.formData.append("file", file.file);
            },
            regist(formName){
                if (this.dialogImageUrl=='') {
                    this.$message.error('请上传头像');
                }
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.formData= new FormData();
                        this.$refs.upload.submit();
                        this.formData.append("username", this.registForm.username);
                        this.formData.append("password", this.registForm.password);
                        this.formData.append("sex", this.registForm.sex);

                        axios({
                            url:'/user/save',
                            method:"post",
                            data:this.formData,
                            headers: {
                                "Content-Type": "multipart/form-data;charset=utf-8"
                            }
                        }).then(ret=>{
                            console.log(ret.data)
                            if(ret.data=="userexist"){
                                this.$message.error('用户名已存在');
                            }
                            if(ret.data=="fail"){
                                this.$message.error('注册失败');
                            }
                            if(ret.data=="success"){
                                this.$message.success('注册成功');
                                this.$router.push('/login')
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },

        },
        computed:{

        }
    }
</script>
